<template>
    <el-card class="box-card" shadow="never">
        <template #header>
            <div class="card-header">
                <span>修改密码</span>
            </div>
        </template>
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
            class="demo-ruleForm">

            <el-form-item label="密码" prop="pwd">
                <el-input v-model="ruleForm.pwd" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="确认密码" prop="confirm">
                <el-input v-model="ruleForm.confirm" type="password" autocomplete="off" />
            </el-form-item>
           
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script setup>
import { reactive, ref } from 'vue'
// import { login } from '../services/user';
import { ElMessage, ElMessageBox } from 'element-plus';
// import {changePwd} from "@/services/user";
import { useRouter } from 'vue-router';
import { useUserStore } from "@/stores/user";
import { storeToRefs } from "pinia";
const userStore = useUserStore();
const { user } = storeToRefs(userStore);
const ruleFormRef = ref();
const router = useRouter();

const ruleForm = reactive({
    pwd: '',
    confirm: '',
})


const validatePwd = (rule, value, callback) => {
    if (ruleForm.confirm !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('confirm', () => null)
    }
    callback()
}
const validateConfirm = (rule, value, callback) => {
  if (value !== ruleForm.pwd) {
    callback(new Error("两次密码不一致"));
  } else {
    callback()
  }
}
const rules = reactive({
    pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { pattern: /^\w{6,50}$/, message:'密码不能少于6位', trigger: 'blur' },
        { validator: validatePwd, trigger: 'blur'}
    ],
    confirm: [
        { required: true, message: '请输入确认密码', trigger: 'blur' },
        { pattern: /^.{6,50}$/, message: '确认密码不能少于6位', trigger: 'blur' },
        { validator: validateConfirm, trigger: 'blur'}
    ]
})
const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            console.log("user:",user.value);
            
            let data = await changePwd(user.value.id,ruleForm.pwd);
            if(data.status == 1){
                ElMessageBox.alert('修改成功，请重新登录！', '提示', {
                    confirmButtonText: '确定',
                    callback: () => {
                        router.push("/login");
                    }
                });

            }else{
                ElMessage.error('修改密码不能和原密码相同')
            }
            
        } else {
            return false
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style>
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.box-card {
    width: 480px;
    margin: auto;
}
</style>